<?php
namespace app\common\controller;
use think\Controller;
use think\Db;
use app\common\logic\SscPayment;
use app\common\logic\KsPayment;
use app\common\logic\SyxwPayment;
use app\common\logic\SslPayment;
use app\common\logic\PcddPayment;
use app\common\logic\PkPayment;
use app\common\logic\QxcPayment;
use app\common\logic\LhcPayment;

class Payment extends Controller {
    protected $time;
    protected $ssc_array     = ['1','2','8'];               //【重庆时时彩,天津时时彩】
    protected $ssl_array      = ['5','7','48'];        //【排列3,福彩3D,上海时时乐】
    protected $syxw_array  = ['29','16','25','28'];    //【江西11选5,山东11选5,广东11选5】
    protected $ks_array       = ['36','44','39','40','41','43','46','47'];          //【安徽快3,江苏快3】
    protected $pcdd_array  = ['10'];                //【新加坡28】
    protected $pk_array      = ['4','61'];            //【北京PK拾,幸运飞艇】
    
    protected $qxc = '51';  // 七星彩
    protected $lhc  = ['9','73'];    // 六合彩
    
//    protected $all_array  = '1,8,5,7,48,16,25,28,36,44,10,4,61,51,9';
    protected $all_array  = '1,2,29,39,40,41,43,46,47,8,48,16,25,28,36,44,10,4,61,73';

    public function _initialize() {
        parent::_initialize();
        set_time_limit(0);
        $this->time = time();
    }
    
    /**
     *处理待结算数据
     */
    public function index() {

        $ctime = microtime(true);
        $cur_time = time();
        $ymd2 = date('Y-m-d H:i:s',$cur_time);
        @file_put_contents('./paymentlog/'.date('Y-m-d',$cur_time).'.txt',"{$ymd2}\n",FILE_APPEND);
		
		
		
        $qxc_start = strtotime(date('Y-m-d',$cur_time).' 20:30:00');
        $qxc_end = strtotime(date('Y-m-d',$cur_time).' 21:00:00');

        $lhc_start = strtotime(date('Y-m-d',$cur_time).' 21:30:00');
        $lhc_end = strtotime(date('Y-m-d',$cur_time).' 22:00:00');

        $fcxd_start = strtotime(date('Y-m-d',$cur_time).' 21:20:00');
        $fcxd_end = strtotime(date('Y-m-d',$cur_time).' 21:50:00');

        $pl3_start = strtotime(date('Y-m-d',$cur_time).' 20:30:00');
        $pl3_end = strtotime(date('Y-m-d',$cur_time).' 21:00:00');

        $count = 0;
        $data_all = Db::name('touzhu_list_no')->alias('t')
		        ->join('__KJJG_LIST__ k','t.czid=k.czid and t.qihao=k.qihao')
		        ->join('__USERNAME__ u','t.username=u.username')
		        ->join('__CAIPAI_LIST__ c','t.czid=c.id')
		        ->field('t.*,k.kjjg,u.id as user_id,u.swzh,u.nbzh as nb,c.cp_name_old,c.cp_name_new')   //k.kjjg务必放在t.*之后
		        ->where('k.kjjg','<>','0')
		        ->where(['t.czid'=>['IN',$this->all_array]])
		        ->limit(1000)
		        ->select();
        $count += count($data_all);
        $this->paymentSingle($data_all);
        if( $cur_time > $qxc_start && $cur_time < $qxc_end ) {
            $lowFrequency = Db::name('touzhu_list_no')->alias('t')
                ->join('__KJJG_LIST__ k','t.czid=k.czid and t.qihao=k.qihao')
                ->join('__USERNAME__ u','t.username=u.username')
                ->join('__CAIPAI_LIST__ c','t.czid=c.id')
                ->field('t.*,k.kjjg,u.id as user_id,u.swzh,u.nbzh as nb,c.cp_name_old,c.cp_name_new')   //k.kjjg务必放在t.*之后
                ->where('k.kjjg','<>','0')
                ->where(['t.czid'=>51])
                ->limit(1000)
                ->select();
            $count += count($lowFrequency);
            $this->paymentSingle($lowFrequency);
        }
        if( $cur_time > $lhc_start && $cur_time < $lhc_end ) {
            $lowFrequency = Db::name('touzhu_list_no')->alias('t')
                ->join('__KJJG_LIST__ k','t.czid=k.czid and t.qihao=k.qihao')
                ->join('__USERNAME__ u','t.username=u.username')
                ->join('__CAIPAI_LIST__ c','t.czid=c.id')
                ->field('t.*,k.kjjg,u.id as user_id,u.swzh,u.nbzh as nb,c.cp_name_old,c.cp_name_new')   //k.kjjg务必放在t.*之后
                ->where('k.kjjg','<>','0')
                ->where(['t.czid'=>9])
                ->limit(1000)
                ->select();
            $count += count($lowFrequency);
            $this->paymentSingle($lowFrequency);
        }
        if( $cur_time > $fcxd_start && $cur_time < $fcxd_end ) {
            $lowFrequency = Db::name('touzhu_list_no')->alias('t')
                ->join('__KJJG_LIST__ k','t.czid=k.czid and t.qihao=k.qihao')
                ->join('__USERNAME__ u','t.username=u.username')
                ->join('__CAIPAI_LIST__ c','t.czid=c.id')
                ->field('t.*,k.kjjg,u.id as user_id,u.swzh,u.nbzh as nb,c.cp_name_old,c.cp_name_new')   //k.kjjg务必放在t.*之后
                ->where('k.kjjg','<>','0')
                ->where(['t.czid'=>7])
                ->limit(1000)
                ->select();
            $count += count($lowFrequency);
            $this->paymentSingle($lowFrequency);
        }
        if( $cur_time > $pl3_start && $cur_time < $pl3_end ) {
            $lowFrequency = Db::name('touzhu_list_no')->alias('t')
                ->join('__KJJG_LIST__ k','t.czid=k.czid and t.qihao=k.qihao')
                ->join('__USERNAME__ u','t.username=u.username')
                ->join('__CAIPAI_LIST__ c','t.czid=c.id')
                ->field('t.*,k.kjjg,u.id as user_id,u.swzh,u.nbzh as nb,c.cp_name_old,c.cp_name_new')   //k.kjjg务必放在t.*之后
                ->where('k.kjjg','<>','0')
                ->where(['t.czid'=>5])
                ->limit(1000)
                ->select();
            $count += count($lowFrequency);
            $this->paymentSingle($lowFrequency);
        }
       
        

        $time = microtime(true)-$ctime;
        $ymd = date('Y-m-d H:i:s',$cur_time);
        @file_put_contents('./paymentlog/'.date('Ymd',$cur_time).'.txt',"{$ymd}，用时{$time}s，可处理{$count}条\n",FILE_APPEND);
       // echo $ymd."<br/>{$time}";
    }
    
    //会员消息发送
    private function sendMessage($username,$cp_name_old,$cp_name_new,$qihao,$zjje) {
        if(!$cp_name_new) $cp_name_new = $cp_name_old;
        $message = "您投注的:【{$cp_name_new}】，【{$qihao}】期已开奖，中奖金额:【{$zjje}】元！";
        $data = array(
        	'username' => $username,
            'test' =>$message ,
            'xx_time' => $this->time,
            'ifdq' => 0
        );
        Db::name('xiaoxi')->data($data)->insert();
    }
    private function paymentSingle($data_all){
        foreach($data_all as $v) {
            if(in_array($v['czid'],$this->ssc_array))
            {
                $ssc = SscPayment::getInstance();
                $payment_result = $ssc->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->ks_array))
            {
                $ks = KsPayment::getInstance();
                $payment_result = $ks->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->syxw_array))
            {
                $syxw = SyxwPayment::getInstance();
                $payment_result = $syxw->dealInfo($v);
                
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->ssl_array))
            {
                $ssl = SslPayment::getInstance();
                $payment_result = $ssl->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->pcdd_array))
            {
                $pcdd = PcddPayment::getInstance();
                $payment_result = $pcdd->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->pk_array))
            {
                $pk =PkPayment::getInstance();
                $payment_result = $pk->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif($v['czid'] == $this->qxc)
            {
                $qxc = QxcPayment::getInstance();
                $payment_result = $qxc->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            }
            elseif(in_array($v['czid'],$this->lhc))
            {
                $lhc = LhcPayment::getInstance();
                $payment_result = $lhc->dealInfo($v);
                if($payment_result !== false) {
                    $this->sendMessage($v['username'],$v['cp_name_old'],$v['cp_name_new'],$v['qihao'],$payment_result);
                }
            } else {}
        }
    }

}
